<?php
/**
 * regd by PhpStorm.
 * User: lee
 * Date: 2017/11/29
 * Time: 17:31
 */

namespace Biz\validate;

use Biz\model\ShippingTempl as mShippingTempl;

class GoodsSku extends \Doraemon\pockets\validate\Base
{


    public static function rules()
    {

        return [

            ['title', 'required', null, '请填写SKU名称', ['create']],
            ['title', 'length', '1,50', 'SKU名称最大50个字符', ['create', 'update']],

            ['price', 'required', null, '请填写SKU价格', ['create', 'update', 'saveSku']],
            ['price', 'between', '0.01,50000', '请正确填写SKU价格', ['create', 'update', 'saveSku']],

            ['stock', 'required', null, '请填写SKU库存', ['create', 'update', 'saveSku']],
            ['stock', 'between', '0,9999', '请正确填写SKU库存', ['create', 'update', 'saveSku']],

          //  ['weight', 'required', null, '请填写重量，默认请填写0', ['create', 'update']],
          //  ['weight', 'between', '0.001,999', '请正确填写SKU重量', ['create', 'update']],
            ['weight', 'callback', ['callback' => [new self(), 'checkWeight']], '按重量计费，需要填写每个SKU的重量', ['create', 'update']],
			['weight', 'required', null, '请填写SKU重量', ['create', 'update']],
			['volume', 'required', null, '请填写SKU体积', ['create', 'update']],
//			['forecast_ship_time', 'required', null, '请填写SKU发货时间', ['create', 'update']],


        ];

    }

    public function checkWeight($value, $valueIdentifier, $allData)
    {
        $shippingTempl = mShippingTempl::getTemplById($allData['uid'], $allData['shipping_templ_id']);

        if(empty($shippingTempl)){
            return false;
        }
        if ($shippingTempl['billing_cate'] == mShippingTempl::BILLING_CATE_WEIGHT) {
            if($value<=0){
                return false;
            }
        }
        return true;
    }

}